package com.lannuokeji.utils.tags.map;

import java.util.Map;

import org.beetl.core.GeneralVarTagBinding;

public class DataRowTag extends GeneralVarTagBinding {

	public static String tagName = "dataRow";

	@Override
	public void render() {
		Map<String, String> attrs = (Map<String, String>) this.args[1];
		// 接收id，如果id不为空，那么按id取值；如果id为空，那么按其他条件查询单条记录
		String id = attrs.get("id");
//		Record dataRow;
		String tableName = attrs.get("table");
		if (id != null) {
//			dataRow = Db.findById(tableName, id);
		} else {
			String querySql = "select ";
			// 接收所要查询的参数，如果为空那么赋值为*
			String getFields = attrs.get("fields");
			String fields = (getFields == null) ? "*" : getFields;
			querySql += fields;

			querySql += " from ";

			// 接收传过来的表名，没有表名不行
			if (tableName == null) {
				return;
			}
			querySql += tableName;
			
			// 接收where需要拼接的条件
			String where = attrs.get("where");
			if (where != null) {
				querySql += " where ";
				querySql += where;
			}
			// 排序
			String orderBy = attrs.get("orderBy");
			if (orderBy != null) {
				querySql += " order by ";
				querySql += orderBy;
			}
//			dataRow = Db.findFirst(querySql);
		}
//		if (dataRow != null && dataRow.getInt("view_count") != null) {
//			dataRow.set("view_count", dataRow.getInt("view_count") + 1);
//			Db.update(tableName, dataRow);
//		}
//		this.binds(dataRow);
		this.doBodyRender();
	}

}